package learning.numbers.numbers;


public class SpanishNumToWord implements IfaceNumToWord {

	/**
	 *  Array 100, 200... 
	 */
	static final String[] hunsNames = {  
     "",
     "cien",
     "docientos",
     "trescientos",
     "cuatrocientos",
     "quinientos",
     "seiscientos",
     "sietecientos",
     "ochocientos",
     "novecientos"		
	};

	/**
	 *  Array 10, 20... 
	 */
	static final String[] tensNames = {
	    "",
	    " diez",
	    " veinte",
	    " treinta",
	    " cuarenta",
	    " cincuenta",
	    " sesenta",
	    " setenta",
	    " ochenta",
	    " noventa"
	  };

	/**
	 *  Array 1, 2... 
	 */
	static final String[] numNames = {

	    "",
	    " uno",
	    " dos",
	    " tres",
	    " cuatro",
	    " cinco",
	    " seis",
	    " siete",
	    " ocho",
	    " nueve",
	    " diez",
	    " once",
	    " doce",
	    " trece",
	    " catorce",
	    " quince",
	    " dieciseis",
	    " diecisiete",
	    " dieciocho",
	    " diecinueve",
	    " veinte",
	    " veintiun",
	    " veintidos",
	    " veintitres",
	    " veinticuatro",
	    " veinticinco",
	    " veintiseis",
	    " veintisiete",
	    " veintiocho",
	    " veintinueve",
	    };
		
	/**
	 * Converts numbers till 999.
	 * @param num  Number to be converted to text.
	 * @return string with the text of num.
	 */
	private String convertHun(int num) {		
		
		String numstring="";
		int hun = num / 100; //3rd number
		int decfin = num % 100;
		int dec = (num % 100) / 10;  //2nd number
		int fin = (num % 100) % 10;  //1st number	
		if (num == 0){ 
			numstring = hunsNames[0];
		}		
		else if (fin == 0) { 
			if (hun==1 && num > 100)
				numstring = "ciento" + " " + tensNames[dec];
			else 
			numstring = hunsNames[hun] + " " + tensNames[dec];
		}
		else if (decfin < 30){ 
			if (hun==1 && num > 100)
				numstring = "ciento" + " " + numNames[decfin];
			else
				numstring = hunsNames[hun] + " " + numNames[decfin];
		}else { 
			if (hun==1 && num > 100)
				numstring = "ciento" + " " + tensNames[dec] + " y " + numNames[fin];
			else
			numstring = hunsNames[hun] + " " + tensNames[dec] + " y " + numNames[fin];
		}
			
		return numstring;
	}
	
	/**
	 * Converts any number till 999 999 
	 * Uses convertHun several times.
	 * @param num   number to be converted.
	 * @return  string with the converted number
	 */
	public String convert(long num) {
		
		int MAX = 6;
		if (num ==0) return "zero";		
		String numstring = Long.toString(num);
		String translated = new String(); 
		int stop = MAX - numstring.length();	
		for (int i=0; i < stop ;i++)
			numstring = '0' + numstring;

		int tho = Integer.parseInt(numstring.substring(0,3));
		int hun = Integer.parseInt(numstring.substring(3,6));
		if (tho == 1 ) { 
			translated = " mil ";
		}
		else if (tho >1) { 
			translated = convertHun(tho) + " mil "; 
		}
		
		translated +=  convertHun(hun);
		return translated.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");	//remove extra spaces 
	}
	
	
    public float comfirmChange(float total, float paid, float change) {
    	
    	if (total == paid + change) {
    		return 1;	
    	}
    	else 
    		return (total-paid);
        	
    }
	
}


